/* 
 * File:   delivery_invoker.h
 * Author: james
 *
 * Created on 2011年3月24日, 下午5:24
 */

#ifndef DELIVERY_INVOKER_H
#define	DELIVERY_INVOKER_H
#include "prepare.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread/thread.hpp>

namespace edm
{
    namespace prepare
    {
        //!
        class delivery_invoker_t: private boost::noncopyable
        {
        public:
            delivery_invoker_t(const char* bind, const char* udp_host, int concurrency);
            ~delivery_invoker_t();

            //!  start: starting delivery invoker service
            //!  @in   void
            //!  @result: int    
            int start(const std::string& maildrop_path,
                      const std::string& maildrop_conf, 
                      callback_fork_t callback_fork,
                      callback_error_t callback_error);

            //!  stop: stopping delivery invoker service, will be waitting all 
            //!        delivery item to be invokedd
            //!  @in   delivery_item_t: item
            //!  @result: int    
            int stop();

            //!  invoke: invoke a maildrop process to delivery the item
            //!  @in   delivery_item_t: item
            //!  @result: void
            void maildrop(delivery_t* delivery);
        private:
            void maildrop_i(delivery_t* delivery);
        private:
            //! bind ip
            std::string m_bind_host;
            //! bind ip
            std::string m_udp_host;
            //! concurrency
            int m_concurrency;
            //! identify whether service is running
            bool m_started;
            //! callback fork
            callback_fork_t m_callback_fork;
            //! callback error
            callback_error_t m_callback_error;
            //! io service object of boost for deadline time and thread working
            boost::asio::io_service m_io_service;
            //! io service work object of boost for keeping io service is running
            boost::scoped_ptr<boost::asio::io_service::work> m_io_work;
            //! TODO::: MayBe multi thread
            //! the service thread of heart beat service
            boost::thread_group m_thread_group;
            //! maildrop path
            std::string m_maildrop_path;
            std::string m_maildrop_conf;

        };
    }
}


#endif	/* DELIVERY_SERVICE_H */

